from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import json

index = 2747
driver = webdriver.Edge()    # Chrome浏览器

def getOnePage():
    driver.get('https://www.hifini.com/forum-1-'+str(index)+'.htm?orderby=lastpid') # 打开url网页 比如 driver.get("http://www.baidu.com")

    list = driver.find_elements(By.XPATH,"//*[@id='body']/div/div/div[1]/div/div[2]/ul/li")
    inde = 1
    listurl = []
    for item in list:
        if index == 1:
            if inde <=3:
                inde = inde+1
                continue
        # driver.get('https://www.hifini.com/forum-1-'+index+'.htm?orderby=lastpid') # 打开url网页 比如 driver.get("http://www.baidu.com")
        oneurl = item.find_elements(By.TAG_NAME,'a')[1].get_attribute('href')
        listurl.append(oneurl)
    print(listurl)
    for item in listurl:
        print(item)
        driver.get(item)
        player = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"player4")))
        sc = driver.find_element(By.XPATH,'//*[@id="body"]/div/div/div[1]/div[1]/div/div[2]/script[3]')
        code = sc.get_attribute('innerHTML')
        formatstr = code[code.index("[")+1:code.index("},")+1]
        formatstr = formatstr.replace("title","'title'")
        formatstr = formatstr.replace("author","'author'")
        formatstr = formatstr.replace("url","'url'")
        formatstr = formatstr.replace("pic","'pic'")
        formatstr = formatstr.replace(" ","")
        formatstr = formatstr.replace("\n","")
        formatstr = formatstr.replace("\r","")
        json1 = eval(formatstr)
        print("https://www.hifini.com/"+json1['url'])


def getOne():
    pass

getOnePage()